//
//  FileManagerUtils.swift
//
//  Created by Ink on 2024/6/23.
//  Copyright © 2024 ZM. All rights reserved.
//

import UIKit

extension FileManager {
    
    public static let libraryPath:String = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]

    public static let cachePath:String = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]

    public static let documentPath:String = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,FileManager.SearchPathDomainMask.userDomainMask,true)[0]

    public static let preferPath:String = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.preferencePanesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]

    /// 沙盒地址
    public static let homeDirPath:String = NSHomeDirectory()
    
    public static let tempDirPath:String = NSTemporaryDirectory()
    
    public static let bundlePath = Bundle.main.bundleURL.path
    
    /// 支付订单相关
    public static let ipaPath = documentPath.append(path: "ipaCache")
    
    /// 用户个人信息
    public static let userInfoPath = documentPath.append(path: "User")
    
    /// 临时下载文件路径
    public static let tempDownloadFileDirPath = tempDirPath.append(path: .netCacheName)
    
    /// 临时下载文件路径
    public static let tempVideoFileDirPath = tempDirPath.append(path: "video")
    
    /// 计算缓存的大小
    public class func cachesSize(path: String) -> UInt {
        let fileManager = FileManager.default
        var fileSize: UInt = 0
        let cachesPath = path
        let files = fileManager.subpaths(atPath: cachesPath)
        if let files = files {
            for fileName: String in files {
                let path = cachesPath.append(path: fileName)
                if fileManager.fileExists(atPath: path) {
                    let fileAttributes = try? fileManager.attributesOfItem(atPath: path) as NSDictionary
                    if let fileAttributes: NSDictionary = fileAttributes {
                        fileSize += UInt(fileAttributes.fileSize())
                    }
                }
            }
        }
        return fileSize
    }
}

extension FileManager {
    
    public class func files(path: String) -> [String]{
        guard let files: [String] = try? FileManager.default.contentsOfDirectory(atPath: path) else {
            return []
        }
        return files
    }
    
    public class func createDirectory(path: String) {
        do {
            try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
        } catch {
            print("\(error)")
        }
    }
    
    /// 删除文件
    public class func removeItem(path: String?) {
        guard let path = path, path.count > 0 else {
            return
        }
        do {
            try FileManager.default.removeItem(atPath: path)
        } catch {
            print("\(error)")
        }
    }
}

extension String {
    /// 拼接路径，确保拼接处有且只有一个“/”
    public func append(path: String) -> String {

        var firstPart = self
        var secondPart = path
        
        while firstPart.hasSuffix("/") { // 去除前部分末尾的所有斜杠
            firstPart.removeLast()
        }
        
        while secondPart.hasPrefix("/") { // 去除后部分开头的斜杠
            secondPart.removeFirst()
        }
        
        // 拼接两部分并用一个斜杠连接
        return "\(firstPart)/\(secondPart)"
    }
}
